#!/bin/bash
source "$SCRIPT_DIR"/build_proxy_cfg.sh
source "$SCRIPT_DIR"/shlog.sh

sigterm_handler() {
  echo "Shutdown signal received."
  exit 1
}

## Setup signal trap
trap 'trap " " SIGINT SIGTERM SIGHUP; kill 0; wait; sigterm_handler' SIGINT SIGTERM SIGHUP

if [ "$BuildUpload2Pgy" == "true" ]; then
  if [ "$BuildAnd" == "true" ]; then
    buildLog "############# begin upload #############"
    cd "$SCRIPT_DIR"
    pwd
    if [[ (-n $AND_APK_FILE_PATH)]]; then
      # upload apk to pgy
      buildLog "AND_APK_FILE_PATH:$AND_APK_FILE_PATH"
      python3 ../jenkins/helper4PgyUpload.py --BuildType=${BuildType} --FilePath=${AND_APK_DIR}${AND_APK_FILE_NAME}
      export AND_APK_QRCODE_FILE_NAME=${AND_APK_FILE_NAME}.qrcode.png
      export AND_APK_QRCODE_FILE_PATH=${AND_APK_DIR}${AND_APK_QRCODE_FILE_NAME}
      # upload ipa tp pgy
      #"${JENKINS_HOME}/bak/${BuildType}/${BUILD_TAG}/and/"
      if [[ "$BuildType" == "pub" && (-n $AND_AAB_FILE_PATH) && (-n $Upload2GooglePlay) ]]; then
        buildLog "upload2GooglePlay bgn AND_AAB_FILE_PATH:$AND_AAB_FILE_PATH"
        cd "$SCRIPT_DIR"
        cd ".."
#        cd "app/build/outputs/bundle/playRelease/"
        echo "-------------------------------------------------------- 切换到aab目录 1 -------------------------------------------------------- --------"
        echo "-------------------------------------------------------- 切换到aab目录 1 -------------------------------------------------------- --------"
        pwd
#        proxy_on
        echo "-------------------------------------------------------- $AND_AAB_FILE_PATH -------------------------------------------------------- --------"
        echo "-------------------------------------------------------- $AND_AAB_FILE_PATH -------------------------------------------------------- --------"
        fastlane upload2GooglePlay aabPath:"$AND_AAB_FILE_PATH"
#        proxy_off
        buildLog "upload2GooglePlay end"
      fi
    elif [ -n "$AND_APK_FILE_PATH" ]; then
      # upload apk to pgy
      buildLog "AND_APK_FILE_PATH:$AND_APK_FILE_PATH"
      python3 ../jenkins/helper4PgyUpload.py --BuildType=${BuildType} --FilePath=${AND_APK_DIR}/${AND_APK_FILE_NAME}
      export AND_APK_QRCODE_FILE_NAME=${AND_APK_FILE_NAME}.qrcode.png
      export AND_APK_QRCODE_FILE_PATH=${AND_APK_DIR}${AND_APK_QRCODE_FILE_NAME}
      if [[ "$BuildType" == "pub" && (-n $AND_AAB_FILE_PATH) && (-n $Upload2GooglePlay) ]]; then
        buildLog "upload2GooglePlay bgn AND_AAB_FILE_PATH:$AND_AAB_FILE_PATH"
        cd "$SCRIPT_DIR"
        cd ".."
#        cd "app/build/outputs/bundle/playRelease/"
        echo "-------------------------------------------------------- 切换到aab目录 2 -------------------------------------------------------- --------"
        echo "-------------------------------------------------------- 切换到aab目录 2 -------------------------------------------------------- --------"
        pwd
#        proxy_on
        echo "-------------------------------------------------------- $AND_AAB_FILE_PATH -------------------------------------------------------- --------"
        echo "-------------------------------------------------------- $AND_AAB_FILE_PATH -------------------------------------------------------- --------"
        fastlane upload2GooglePlay aabPath:"$AND_AAB_FILE_PATH"
#        proxy_off
        buildLog "upload2GooglePlay end"
      fi
    fi
    buildLog "############# end upload #############"

    sh $SCRIPT_DIR/artifact_notify.sh
  fi
fi
